package ks.client.gui.admin.plugin.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

import ks.client.controllers.admincommands.PluginInstallCommand;
import ks.client.controllers.commands.PluginListCommandController;
import ks.client.gui.ErrorMessageGUI;
import ks.client.kommander.Action;
import ks.client.lobby.Lobby;


/**
 * 
 * Listens for the "Install" button to be clicked
 * and creates a PluginInstallCommandController to send
 * the plugin-install command
 * 
 * @author Jennifer Hunt
 *
 */
public class InstallClick implements ActionListener{
	
	Lobby lobby;
	//URL of the plugin being installed
	JTextField url;
	
	/**
	 * Constructor for InstallClick.
	 * 
	 * @param lobby
	 * @param table
	 */
	public InstallClick(Lobby lobby, JTextField url) {
		this.lobby = lobby;
		this.url = url;
	}

	/**
	 * 
	 * Button is clicked. If the URL field is empty the error message gui is called.
	 * Otherwise a PluginInstallCommand is created to send the message to install the plugin.
	 * A PluginListCommand is then sent to update the plugin lists on all the clients. 
	 * 
	 */
	public void actionPerformed(ActionEvent e){
		if(url.getText().isEmpty()){
				JFrame pma = new ErrorMessageGUI("Please input a plugin url.");
				pma.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				pma.setTitle("Error");
				pma.setVisible(true);
			}
		else{
			new PluginInstallCommand(lobby).send(url.getText(), new Action() {
				public void ack(){
					new PluginListCommandController(lobby);
				}
				public void nack(){
				
				}});
			
		
		}
	}
}
